import 'package:flutter/material.dart';
import 'package:flutter_wanandroid_ohos/common/index.dart';
import 'package:flutter_wanandroid_ohos/pages/simple/channel/channel_view.dart';
import 'package:flutter_wanandroid_ohos/routes/route_key.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold_ohos/common/index.dart';

import 'simple_logic.dart';

class SimplePage extends StatefulWidget {
  const SimplePage({super.key});

  @override
  State<SimplePage> createState() => _SimplePageState();
}

class _SimplePageState extends State<SimplePage>
    with AutomaticKeepAliveClientMixin {
  Widget _buildView() {
    return <Widget>[
      // ListTile(
      //   title: Text(TextKey.tdesign.tr),
      //   onTap: () {
      //     Get.toNamed(RouteKey.theme);
      //   },
      // ),
      ListTile(
        title: Text(TextKey.zhuTi.tr),
        onTap: () {
          Get.toNamed(RouteKey.theme);
        },
      ),
      ListTile(
        title: Text(TextKey.yuYan.tr),
        onTap: () {
          Get.toNamed(RouteKey.language);
        },
      ),
      ListTile(
        title: Text(TextKey.jiChuZuJian.tr),
        onTap: () {
          Get.toNamed(RouteKey.baseWidgets);
        },
      ),
      ListTile(
        title: Text(TextKey.jiZaiRongQiZuJian.tr),
        onTap: () {
          Get.toNamed(RouteKey.loadContainer);
        },
      ),
      ListTile(
        title: Text(TextKey.shiYongZhuTiSe.tr),
        onTap: () {
          Get.toNamed(RouteKey.themeColor);
        },
      ),
      ListTile(
        title: Text(TextKey.duiHuaKuang.tr),
        onTap: () {
          Get.toNamed(RouteKey.dialog);
        },
      ),
      ListTile(
        title: Text(TextKey.wangLuoQingQiu.tr),
        onTap: () {
          Get.toNamed(RouteKey.network);
        },
      ),
      ListTile(
        title: Text(TextKey.shenQingQuanXian.tr),
        onTap: () {
          Get.toNamed(RouteKey.permission);
        },
      ),
      ListTile(
        title: Text(TextKey.huoQuDangQianShiJianChuo.tr),
        onTap: () async {
          showToast(getTimeStamp(isSecond: true).toString());
        },
      ),
      ListTile(
        title: Text(TextKey.shiJianChuoZhuanRiQi.tr),
        onTap: () async {
          showToast(getTimeStamp().toDateTimeString() ?? '');
        },
      ),
      ListTile(
        title: Text(TextKey.md5JiaMi.tr),
        onTap: () async {
          showToast(getNowDateString().md5() ?? '');
        },
      ),
      ListTile(
        title: Text(TextKey.base64BianMa.tr),
        onTap: () async {
          showToast(getNowDateString().encodeBase64() ?? '');
        },
      ),
      ListTile(
        title: Text(TextKey.yingYongBanBen.tr),
        onTap: () async {
          String version = await getVersion();
          String build = await getBuildNumber();
          showToast('version:$version build:$build');
        },
      ),
      ListTile(
        title: Text(TextKey.sheBeiXinXi.tr),
        onTap: () async {
          var infos = await getDeviceInfo();
          log(infos.toString());
          String deviceName = await getDeviceModel() ?? 'unknown';
          String version = '';
          if (PlatformUtil.isAndroid) {
            int? code = await getAndroidSdkVersion();
            version = code?.toString() ?? 'unknown';
          }
          if (PlatformUtil.isIOS) {
            version = await getIosSystemVersion() ?? 'unknown';
          }
          if (PlatformUtil.isOhos) {
            int? code = await getOhosSdkVersion();
            version = code?.toString() ?? 'unknown';
          }
          showToast('$deviceName $version');
        },
      ),
      ListTile(
        title: Text(TextKey.queRenWangLuoLianJie.tr),
        onTap: () async {
          if (await isNetworkAvailable()) {
            showSuccessToast('Available');
          } else {
            showErrorToast('Unavailable');
          }
        },
      ),
      ListTile(
        title: Text(TextKey.daKaiWangYe.tr),
        onTap: () async {
          openWebPage('https://www.baidu.com',
              mode: LaunchMode.externalApplication);
        },
      ),
      ListTile(
        title: Text(TextKey.boDaDianHua.tr),
        onTap: () async {
          callPhone('13888888888');
        },
      ),
      ListTile(
        title: Text(TextKey.xianShiLoading.tr),
        onTap: () async {
          showLoading('Loading...');
          delayed(3000, () {
            dismissLoading();
          });
        },
      ),
      ListTile(
        title: Text(TextKey.xianShiError.tr),
        onTap: () async {
          showError('ERROR 500');
        },
      ),
      ListTile(
        title: Text(TextKey.qingChuTuPianHuanCun.tr),
        onTap: () async {
          if (await clearDiskCachedImages()) {
            showSuccessToast('Success');
          } else {
            showErrorToast('Error');
          }
        },
      ),
      const ListTileX(
        title: 'ListTileX',
        subTitle: 'subTitle',
      ),
      ListTileX(
        title: 'Pigeon Example',
        subTitle: 'Pigeon Example',
        onTap: () {
          Get.to(ChannelPage());
        },
      ),
    ].toListView(separator: const DividerX()).scrollbar().safeArea();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return GetBuilder<SimpleLogic>(
        init: SimpleLogic(),
        id: 'simple',
        builder: (logic) {
          return Scaffold(
            appBar: AppBar(
              title: Text(TextKey.tab_simple.tr),
              centerTitle: true,
              elevation: 1,
            ),
            body: _buildView(),
          );
        });
  }

  @override
  bool get wantKeepAlive => true;
}
